<template>
  <div class="params">
    <!-- 1. 目录位置 -->
    <div class="nav">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/params' }"
          >规格参数</el-breadcrumb-item
        >
        <el-breadcrumb-item>规格包装</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 2. 搜索框 -->
    <div class="header">
      <el-input
        v-model="inp"
        @change="searchParams"
        placeholder="请输入内容,只可以查询规格参数内容"
      />
      <el-button type="primary" icon="el-icon-search">查看</el-button>
      <el-button type="primary" icon="el-icon-plus" @click="showParams"
        >添加</el-button
      >
    </div>

    <!-- 3. 表格 -->
    <el-table :data="tableData" class="my-table">
      <el-table-column prop="itemCatId" label="规格参数ID" width="120">
      </el-table-column>
      <el-table-column prop="id" label="类目ID" width="120"> </el-table-column>
      <el-table-column prop="specsName" label="规格名称" width="120">
      </el-table-column>
      <el-table-column prop="paramData" label="规格参数" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
          >
            编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 4. 分页 -->
    <MyPagination
      :total="total"
      :pageSize="pageSize"
      @changePage="changePage"
    />
    <!-- 5. 弹框 -->
    <ParamsDialog ref="dialog" />
    <!-- 6. 修改弹框 -->
    <params-edit ref="edit" :rowData="rowData"/>
  </div>
</template>

<script>
import MyPagination from "../../../components/MyPagination.vue";
import ParamsDialog from "./ParamsDialog.vue";
import ParamsEdit from "./ParamsEdits.vue";
export default {
  components: {
    MyPagination,
    ParamsDialog,
    ParamsEdit,
  },
  data() {
    return {
      inp: "", //输入框
      tableData: [],
      total: 10,
      pageSize: 1,
      type: 1,
      currentPage: 1,
      list: [],
      title: "添加规格",
      rowData: {}, //当前行的数据对象
      params:""
    };
  },

  methods: {
    /**
     * 点击删除--配置规格参数
     */
    handleDelete(index, row) {
      console.log("删除", index, row);
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //请求接口----
          this.$api
            .deleteParams({
              id: row.id,
            })
            .then((res) => {
              console.log("删除", res.data);
              if (res.data.status === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                //视图更新
                this.http(1);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /**
     * 点击修改--配置规格参数
     */
    handleEdit(index,row){
      console.log("删除", index, row);
      this.$refs.edit.dialogVisible = true;
      this.rowData = { ...row };
    },
    /**
     * 点击搜索--配置规格参数
     */
    searchParams(val) {
      if (!val) {
        this.http(1);
        this.currentPage = 1;
        this.type = 1;
        return;
      }
      this.$api
        .searchParams({
          search: val,
        })
        .then((res) => {
          console.log(res);
          if (res.data.status === 200) {
            this.list = res.data.result; //获取的搜索的总数据条数---数据分割
            //假设需要分页---我们处理分页----
            this.total = res.data.result.length;
            this.pageSize = 3;
            this.tableData = res.data.result.slice(0, 3);
            this.type = 2;
            console.log("分页", this.currentPage);
          } else {
            this.tableData = [];
            this.total = 1;
            this.pageSize = 1;
            this.type = 1;
          }
        });
    },
    /**
     * 点击添加弹框--配置规格参数
     */
    showParams() {
      this.$refs.dialog.dialogVisible = true;
    },
    /**
     * 点击分页-切换
     */
    changePage(num) {
      this.currentPage = num;
      if (this.type == 1) {
        this.http(num); //商品列表分页
      } else {
        //搜索分页 1 2 3 4 --  list=[0,1,2,3,4,5,6,7,8]  0-3  3-6  6-9 9-12
        console.log("搜索的分页处理---");
        //（num-1）*3  num*3
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    /**
     * 获取规格参数列表
     */
    http(page) {
      this.$api.getParams({ page }).then((res) => {
        console.log(res.data);
        if (res.data.status === 200) {
          this.tableData = res.data.data;
          //获取分页
          this.total = res.data.total;
          this.pageSize = res.data.pageSize;
        }
      });
    },
  },
  created() {
    this.http(1);
  },
};
</script>

<style lang='less' scoped>
.params {
  margin: 10px;
}
.nav {
  padding: 10px;
}
.header {
  display: flex;
  background: #fff;
  padding: 10px;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
}
.my-table {
  margin: 10px auto;
}
</style>